from django.db import models
from django.db.models.signals import post_delete
from django.dispatch import receiver
from django.db.models import F

# Create your models here.

class UserModel(models.Model):
    nickname = models.CharField(max_length=100, verbose_name='昵称')
    password = models.CharField(max_length=100, verbose_name='密码')
    avatar = models.ImageField(upload_to='avatar/', verbose_name='头像')
    email = models.EmailField(max_length=100, verbose_name='邮箱', unique=True)
    introduce = models.CharField(max_length=100, verbose_name='简介')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    def __str__(self):
        return self.nickname
    class Meta:
        db_table = 'user'
        verbose_name = '用户'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']
# 注册时的验证码
class CaptchaModel(models.Model):
    email = models.EmailField(unique=True)
    captcha = models.CharField(max_length=6)
    created_time = models.DateTimeField(auto_now_add=True)

# 帖子
class PostModel(models.Model):
    categpry_choice = (
        ('1', '体育'),
        ('2', '音乐'),
        ('3', '学习'),
    )
    title = models.CharField(max_length=100, verbose_name='标题')
    content = models.TextField(verbose_name='内容')
    category = models.CharField(max_length=100, verbose_name='分类', choices=categpry_choice)
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='作者')
    like_count = models.IntegerField(default=0, verbose_name='点赞数')
    comment_count = models.IntegerField(default=0, verbose_name='评论数')
    favorite_count = models.IntegerField(default=0, verbose_name='收藏数')
    def __str__(self):
        return self.title
    class Meta:
        db_table = 'post'
        verbose_name = '帖子'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']


class LikeModel(models.Model):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, related_name='likes')
    post = models.ForeignKey(PostModel, on_delete=models.CASCADE, related_name='likes')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='点赞时间')

    class Meta:
        db_table = 'like'
        verbose_name = '点赞'
        verbose_name_plural = verbose_name
        unique_together = ('user', 'post')  # 确保每个用户对每个帖子只能点赞一次

class CommentModel(models.Model):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, related_name='comments')
    post = models.ForeignKey(PostModel, on_delete=models.CASCADE, related_name='comments')
    content = models.TextField(verbose_name='评论内容')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='评论时间')

    def __str__(self):
        return f'Comment by {self.user.nickname} on {self.post.title}'

    class Meta:
        db_table = 'comment'
        verbose_name = '评论'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']
@receiver(post_delete, sender=CommentModel)
def update_post_comment_count(sender, instance, **kwargs):
    # 在评论被删除后，更新关联帖子的评论数量
    instance.post.comment_count = F('comment_count') - 1  # 使用F表达式递减评论数量
    instance.post.save()

class FavoriteModel(models.Model):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, related_name='favorites')
    post = models.ForeignKey(PostModel, on_delete=models.CASCADE, related_name='favorites')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='收藏时间')
    class Meta:
        db_table = 'favorite'
        verbose_name = '收藏'
        verbose_name_plural = verbose_name
        unique_together = ('user', 'post')